/**
 * Created by TamTT on 1/13/14.
 * FPT University - Da Nang
 * Capstone project - group 3
 * Jornee
 */
module.exports.controller = function(app, jwt, util, secret, db, authentication, async, log, colors){
	var mongojs = require('mongojs');

    /**
     * Host register - ThanhND
     */

    app.post('/host_register', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var country = req.body.country.fromVi();
                var administrative_area_level_1 = req.body.administrative_area_level_1.fromVi();
                var locality = req.body.locality.fromVi();
                var active_start = req.body.active_start;
                var active_end = req.body.active_end;
                var phone = req.body.phone;
                var description = req.body.description;
                var place_about = req.body.place_about;
                var place_id = "";
                var question = new Array();
                db.place.findOne(
                    { country: country, administrative_area_level1: administrative_area_level_1, locality: locality, route: ""}, function (err, doc) {
                        if (err) {
                            log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                            res.send({authen_status: "ok", status: "error"});
                        } else {
                            if (doc) {
                                place_id = doc._id;
                                db.host.insert(
                                    {
                                        user_id: result.login_user,
                                        place_id: place_id,
                                        description: description,
                                        active_time: {active_start: active_start, active_end: active_end},
                                        create_date: new Date().toISOString(),
                                        modified_date: '',
                                        status: true,
                                        rate: [],
                                        feedback: [],
                                        about_place: place_about
                                    }, function (err) {
                                        if (err) {
                                            log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                            res.send({authen_status: 'ok', status: 'error'});
                                        } else {
                                            db.users.update({username: result.login_user}, {$set: {host: true}}, function (err) {
                                                if (err) {
                                                    log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                    res.send({authen_status: 'ok', status: 'error'});
                                                } else {
                                                    db.tipsquestion.find({}, {content: 1, _id: 0}, function (err, doc) {
                                                        if (err) {
                                                            log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                            res.send({authen_status: 'ok', status: 'error'});
                                                        }
                                                        if (!doc) {
                                                            log.warning('Host register - User %s - No tip question found'.yellow, result.login_user);
                                                            res.send({authen_status: 'ok', status: 'fail'});
                                                        }
                                                        else {
                                                            question = doc;
                                                            var tips = [];
                                                            question.forEach(function (content) {
                                                                tips.push({question: content.content, answer: ""});
                                                            })
                                                            db.host.update({user_id: result.login_user}, {$set: {tips: tips}}, function (err) {
                                                                if (err) {
                                                                    log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                                    res.send({authen_status: 'ok', status: 'fail'});
                                                                } else {
                                                                    log.info('Host register - User %s - OK'.blue, result.login_user);
                                                                    res.send({ authen_status: 'ok', status: 'ok', tip_question: question });
                                                                }
                                                            })
                                                        }
                                                    });
                                                }
                                            });
                                        }
                                    }
                                );
                            } else {
                                db.place.insert(
                                    { country: country, administrative_area_level1: administrative_area_level_1, locality: locality, route: "", create_date: new Date().toISOString()}, function (err, doc) {
                                        if (err) {
                                            log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                            res.send({authen_status: "ok", status: "error"});
                                        }
                                        if (!doc) {
                                            log.warning('Host register - User %s - No place found'.yellow, result.login_user);
                                            res.send({authen_status: 'ok', status: 'fail'});
                                        } else {
                                            place_id = doc._id;
                                            db.host.insert(
                                                {
                                                    user_id: result.login_user,
                                                    place_id: place_id,
                                                    description: description,
                                                    active_time: {active_start: active_start, active_end: active_end},
                                                    create_date: new Date().toISOString(),
                                                    modified_date: '',
                                                    status: true,
                                                    rate: [],
                                                    feedback: [],
                                                    about_place: place_about
                                                }, function (err) {
                                                    if (err) {
                                                        log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                        res.send({authen_status: 'ok', status: 'error'});
                                                    } else {
                                                        db.users.update({username: result.login_user}, {$set: {host: true}}, function (err) {
                                                            if (err) {
                                                                log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                                res.send({authen_status: 'ok', status: 'error'});
                                                            } else {
                                                                db.tipsquestion.find({}, {content: 1, _id: 0}, function (err, doc) {
                                                                    if (err) {
                                                                        log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                                        res.send({authen_status: 'ok', status: 'error'});
                                                                    }
                                                                    if (!doc) {
                                                                        log.warning('Host register - User %s - No tip question found'.yellow, result.login_user);
                                                                        res.send({authen_status: 'ok', status: 'fail'});
                                                                    } else {
                                                                        question = doc;
                                                                        var tips = [];
                                                                        question.forEach(function (content) {
                                                                            tips.push({question: content.content, answer: ""});
                                                                        })
                                                                        db.host.update({user_id: result.login_user}, {$set: {tips: tips}}, function (err) {
                                                                            if (err) {
                                                                                log.error('Host register - User %s - Error with %s'.red, result.login_user, err);
                                                                                res.send({authen_status: 'ok', status: 'fail'});
                                                                            } else {
                                                                                log.info('Host register - User %s - OK'.blue, result.login_user);
                                                                                res.send({ authen_status: 'ok', status: 'ok', tip_question: question });
                                                                            }
                                                                        })
                                                                    }
                                                                });
                                                            }
                                                        });
                                                    }
                                                }
                                            );
                                        }
                                    }
                                );
                            }
                        }
                    });
            }
        });
    });

    /**
     * Check host - ThanhND
     */
    app.post('/check_host', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                db.users.findOne({username: result.login_user, host: true}, function (err, doc) {
                    if (err) {
                        log.error('Check host - User %s - Error with %s'.red, result.login_user, err);
                        res.send({ authen_status: 'ok', status: 'error'});
                    } else {
                        if (doc) {
                            log.info('Check host - User %s - OK'.blue, result.login_user);
                            res.send({authen_status: 'ok', status: 'host'});
                        } else {
                            log.info('Check host - User %s - Not'.blue, result.login_user);
                            res.send({authen_status: 'ok', status: 'not'});
                        }
                    }
                });
            }
        });
    });

    app.post('/add_tips', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var return_ans = req.body.answer;
                var answer = eval(return_ans.slice(0, return_ans.length - 2) + ']');

                db.host.update({user_id: result.login_user}, {$set: {tips: answer}}, function (err) {
                    if (err) {
                        log.error('Add tips - User %s - Error with %s'.red, result.login_user, err);
                        res.send({authen_status: 'ok', status: 'error'});
                    } else {
                        log.info('Add tips - User %s - OK'.blue, result.login_user);
                        res.send({authen_status: 'ok', status: 'ok'});
                    }
                });
            }
        });
    });

    app.post('/host_profile', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                db.host.findOne({user_id: result.login_user}, function (err1, doc1) {
                    if (err1) {
                        log.error('Host profile - User %s - Error with %s'.red, result.login_user, err1);
                        res.send({authen_status: 'ok', status: 'error'});
                    }
                    if (!doc1) {
                        log.warning('Host profile - User %s - No host found'.yellow, result.login_user);
                        res.send({authen_status: 'ok', status: 'fail'});
                    } else {
                        db.users.findOne({username: result.login_user}, function (err2, doc2) {
                            if (err2) {
                                log.error('Host profile - User %s - Error with %s'.red, result.login_user, err2);
                                res.send({authen_status: 'ok', status: 'error'});
                            }
                            if (!doc2) {
                                log.warning('Host profile - User %s - No user found'.yellow, result.login_user);
                                res.send({authen_status: 'ok', status: 'fail'});
                            } else {
                                db.place.findOne({_id: doc1.place_id}, function (err3, doc3) {
                                    if (err3) {
                                        log.error('Host profile - User %s - Error with %s'.red, result.login_user, err3);
                                        res.send({authen_status: 'ok', status: 'error'});
                                    }
                                    if (!doc3) {
                                        log.warning('Host profile - User %s - No place found'.yellow, result.login_user);
                                        res.send({authen_status: 'ok', status: 'fail'});
                                    } else {
                                        var location = "";
                                        if (doc3.country != null && doc3.country != "") {
                                            location = doc3.country;
                                        }
                                        if (doc3.administrative_area_level1 != null && doc3.administrative_area_level1 != "") {
                                            location = doc3.administrative_area_level1;
                                        }
                                        if (doc3.locality != null && doc3.locality !== "") {
                                            location = doc3.locality;
                                        }
                                        var star = [0, 0, 0 , 0, 0];
                                        async.each(doc1.rate,
                                            function (rate, callback) {
                                                star[rate.rate - 1] = star[rate.rate - 1] + 1;
                                                callback();
                                            },
                                            function (err) {
                                                if (doc1.rate.length == 0) {
                                                    res.send({authen_status: 'ok', status: 'ok', username: doc1.user_id, avatar: doc2.avatar, email: doc2.email, location: location, active_status: doc1.status, active_start: doc1.active_time.active_start, active_end: doc1.active_time.active_end, DoB: doc2.DoB, gender: doc2.gender, description: doc1.description, place_id: doc1.place_id, about_place: doc1.about_place, rate: [
                                                        {one: star[0]},
                                                        {two: star[1]},
                                                        { three: star[2]},
                                                        {four: star[3]},
                                                        { five: star[4]},
                                                        { everage: 0},
                                                        {number_of_rate: 0}
                                                    ], feedback: doc1.feedback, tips: doc1.tips});
                                                } else {
                                                    var rate_everage = (star[0] + star[1] * 2 + star[2] * 3 + star[3] * 4 + star[4] * 5) / (doc1.rate.length);
                                                    log.info('Host profile - User %s - OK'.blue, result.login_user);
                                                    res.send({authen_status: 'ok', status: 'ok', username: doc1.user_id, avatar: doc2.avatar, email: doc2.email, location: location, active_status: doc1.status, active_start: doc1.active_time.active_start, active_end: doc1.active_time.active_end, DoB: doc2.DoB, gender: doc2.gender, description: doc1.description, place_id: doc1.place_id, about_place: doc1.about_place, rate: [
                                                        {one: star[0]},
                                                        {two: star[1]},
                                                        { three: star[2]},
                                                        {four: star[3]},
                                                        { five: star[4]},
                                                        { everage: rate_everage},
                                                        {number_of_rate: doc1.rate.length}
                                                    ], feedback: doc1.feedback, tips: doc1.tips});
                                                }
                                            }
                                        );
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    });

    app.post('/other_host_profile', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var host_username = req.body.host_username;
                db.host.findOne({user_id: host_username}, function (err1, doc1) {
                    if (err1) {
                        log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err1);
                        res.send({authen_status: 'ok', status: 'error'});
                    }
                    if (!doc1) {
                        log.warning('Other host profile - User %s - No host found'.yellow, result.login_user);
                        res.send({authen_status: 'ok', status: 'fail'});
                    } else {
                        db.users.findOne({username: host_username}, function (err2, doc2) {
                            if (err2) {
                                log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err2);
                                res.send({authen_status: 'ok', status: 'error'});
                            }
                            if (!doc2) {
                                log.warning('Other host profile - User %s - No user found'.yellow, result.login_user);
                                res.send({authen_status: 'ok', status: 'fail'});
                            } else {
                                db.place.findOne({_id: doc1.place_id}, function (err3, doc3) {
                                    if (err3) {
                                        log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err3);
                                        res.send({authen_status: 'ok', status: 'error'});
                                    }
                                    if (!doc3) {
                                        log.warning('Other host profile - User %s - No place found'.yellow, result.login_user);
                                        res.send({authen_status: 'ok', status: 'fail'});
                                    } else {
                                        var location = "";
                                        if (doc3.country != null && doc3.country != "") {
                                            location = doc3.country;
                                        }
                                        if (doc3.administrative_area_level1 != null && doc3.administrative_area_level1 != "") {
                                            location = doc3.administrative_area_level1;
                                        }
                                        if (doc3.locality != null && doc3.locality !== "") {
                                            location = doc3.locality;
                                        }
                                        var star = [0, 0, 0 , 0, 0];
                                        async.each(doc1.rate,
                                            function (rate, callback) {
                                                star[rate.rate - 1] = star[rate.rate - 1] + 1;
                                                callback();
                                            },
                                            function (err) {
                                                if (err) {
                                                    log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err);
                                                    res.send({authen_status: 'ok', status: 'error'});
                                                } else {
                                                    db.host.find({user_id: host_username}, {rate: {$elemMatch: {user_id: result.login_user}}}, function (err5, doc5) {
                                                        if (err5) {
                                                            log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err5);
                                                            res.send({authen_status: 'ok', status: 'error'});
                                                        }
                                                        else {
                                                            if (doc5.rate == null) {
                                                                doc5.rate = [""];
                                                            }
                                                            db.host.find({user_id: host_username}, {feedback: {$elemMatch: {username: result.login_user}}}, function (err6, doc6) {
                                                                if (err6) {
                                                                    log.error('Other host profile - User %s - Error with %s'.red, result.login_user, err6);
                                                                    res.send({authen_status: 'ok', status: 'error'});
                                                                }
                                                                else {
                                                                    if (doc6.feedback == null) {
                                                                        doc6.feedback = [""];
                                                                    }
                                                                    if (doc1.rate.length == 0) {
                                                                        res.send({authen_status: 'ok', status: 'ok', username: doc1.user_id, avatar: doc2.avatar, email: doc2.email, location: location, active_status: doc1.status, active_start: doc1.active_time.active_start, active_end: doc1.active_time.active_end, DoB: doc2.DoB, gender: doc2.gender, description: doc1.description, place_id: doc1.place_id, about_place: doc1.about_place, rate: [
                                                                            {one: star[0]},
                                                                            {two: star[1]},
                                                                            { three: star[2]},
                                                                            {four: star[3]},
                                                                            { five: star[4]},
                                                                            { everage: 0},
                                                                            {number_of_rate: 0}
                                                                        ], feedback: doc1.feedback, tips: doc1.tips, user_rate: doc5.rate[0].rate, user_feedback: doc6.feedback[0].feedback});
                                                                    } else {
                                                                        var rate_everage = (star[0] + star[1] * 2 + star[2] * 3 + star[3] * 4 + star[4] * 5) / (doc1.rate.length);
                                                                        log.info('Other host profile - User %s - OK'.blue, result.login_user);
                                                                        res.send({authen_status: 'ok', status: 'ok', username: doc1.user_id, avatar: doc2.avatar, email: doc2.email, location: location, active_status: doc1.status, active_start: doc1.active_time.active_start, active_end: doc1.active_time.active_end, DoB: doc2.DoB, gender: doc2.gender, description: doc1.description, place_id: doc1.place_id, about_place: doc1.about_place, rate: [
                                                                            {one: star[0]},
                                                                            {two: star[1]},
                                                                            { three: star[2]},
                                                                            {four: star[3]},
                                                                            { five: star[4]},
                                                                            { everage: rate_everage},
                                                                            {number_of_rate: doc1.rate.length}
                                                                        ], feedback: doc1.feedback, tips: doc1.tips, user_rate: doc5.rate[0].rate, user_feedback: doc6.feedback[0].feedback});
                                                                    }
                                                                }
                                                            });
                                                        }
                                                    });
                                                }
                                            }
                                        );
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    });

    app.post('/edit_host_location', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var country = req.body.country.fromVi();
                var administrative_area_level_1 = req.body.administrative_area_level_1.fromVi();
                var locality = req.body.locality.fromVi();
                var about_place = req.body.about_place;
                db.place.findOne(
                    { country: country, administrative_area_level1: administrative_area_level_1, locality: locality, route: ""}, function (err, doc) {
                        if (err) {
                            log.error('Edit host location - User %s - Error with %s'.red, result.login_user, err);
                            res.send({authen_status: "ok", status: "error"});
                        } else {
                            if (doc) {
                                db.host.update(
                                    {user_id: result.login_user},
                                    {$set: {
                                        place_id: doc._id,
                                        about_place: about_place,
                                        modified_date: new Date()
                                    } }, function (err) {
                                        if (err) {
                                            log.error('Edit host location - User %s - Error with %s'.red, result.login_user, err);
                                            res.send({authen_status: "ok", status: "error"});
                                        } else {
                                            log.info('Edit host location - User %s - OK'.blue, result.login_user);
                                            res.send({authen_status: "ok", status: "ok"});
                                        }
                                    }
                                );
                            } else {
                                db.place.insert(
                                    { country: country, administrative_area_level1: administrative_area_level_1, locality: locality, route: "", create_date: new Date().toISOString()}, function (err, doc) {
                                        if (err) {
                                            log.error('Edit host location - User %s - Error with %s'.red, result.login_user, err);
                                            res.send({authen_status: "ok", status: "error"});
                                        }
                                        if (!doc) {
                                            log.warning('Edit host location - User %s - No place found'.yellow, result.login_user);
                                            res.send({authen_status: 'ok', status: 'fail'});
                                        } else {
                                            db.host.update(
                                                {user_id: result.login_user},
                                                {$set: {
                                                    place_id: doc._id,
                                                    about_place: about_place,
                                                    modified_date: new Date()
                                                }}, function (err) {
                                                    if (err) {
                                                        log.error('Edit host location - User %s - Error with %s'.red, result.login_user, err);
                                                        res.send({authen_status: "ok", status: "error"});
                                                    } else {
                                                        log.info('Edit host location - User %s - OK'.blue, result.login_user);
                                                        res.send({authen_status: "ok", status: "ok"});
                                                    }
                                                }
                                            );
                                        }
                                    }
                                );
                            }
                        }
                    });
            }
        });
    });

    app.post('/host_activate', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                db.host.update({user_id: result.login_user}, {$set: {status: true}}, function (err) {
                    if (err) {
                        log.error('Activate host- User %s - Error with %s'.red, result.login_user,err);
                        res.send({authen_status: 'ok', status: 'error'});
                    } else {
                        log.info('Activate host- User %s - OK'.blue, result.login_user);
                        res.send({authen_status: 'ok', status: 'ok'});
                    }
                });

            }
        });
    });

    app.post('/host_deactivate', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                db.host.update({user_id: result.login_user}, {$set: {status: false}}, function (err) {
                    if (err) {
                        log.error('Deactivate host- User %s - Error with %s'.red, result.login_user,err);
                        res.send({authen_status: 'ok', status: 'error'});
                    } else {
                        log.info('Deactivate host- User %s - OK'.blue, result.login_user);
                        res.send({authen_status: 'ok', status: 'ok'});
                    }
                });

            }
        });
    });

    app.post('/edit_host_description', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var description = req.body.description;
                var about_place = req.body.about_place;
                db.host.findOne({user_id: result.login_user}, function (err, doc) {
                    if (err) {
                        log.error('Edit host description- User %s - Error with %s'.red, result.login_user,err);
                        res.send({authen_status: 'ok', status: 'error'});
                    }
                    if (!doc) {
                        log.warning('Edit host description- User %s - No host found'.yellow, result.login_user);
                        res.send({authen_status: 'ok', status: 'error'});
                    }
                    else {
                        db.host.update({user_id: result.login_user}, {$set: {description: description, about_place: about_place}}, function (err) {
                            if (err) {
                                log.error('Edit host description- User %s - Error with %s'.red, result.login_user,err);
                                res.send({authen_status: 'ok', status: 'error'});
                            } else {
                                log.info('Edit host description- User %s - OK'.blue, result.login_user);
                                res.send({authen_status: 'ok', status: 'ok'});
                            }
                        });
                    }
                });
            }
        });
    });

    app.post('/edit_active_time', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var active_start = req.body.active_start;
                var active_end = req.body.active_end;
                db.host.update({user_id: result.login_user}, {$set: {active_time: {active_start: active_start, active_end: active_end}}}, function (err) {
                    if (err) {
                        log.error('Edit active time - User %s - Error with %s'.red, result.login_user,err);
                        res.send({authen_status: 'ok', status: 'error'});
                    } else {
                        log.info('Edit active time - User %s - OK'.blue, result.login_user);
                        res.send({authen_status: 'ok', status: 'ok'});
                    }
                });
            }
        });
    });

    app.post('/feedback', function (req, res) {
        authentication(req, function (result) {
            if (result.loginStatus == 'error') {
                res.send({ authen_status: 'error' });
            } else if (result.loginStatus == 'false') {
                res.send({ authen_status: 'fail' });
            } else if (result.loginStatus == 'true') {
                /**
                 * Code begin
                 */
                var host_username = req.body.host_username;
                var feedback = req.body.feedback;
                var rate = req.body.rate;

                db.host.update({user_id: host_username}, {$pull: {rate: {user_id: result.login_user}}}, function (err) {
                    if (err) {
                        log.error('Feedback - User %s - Error with %s'.red, result.login_user,err);
                        res.send({authen_status: 'ok', status: 'error'});
                    } else {
                        db.host.update({user_id: host_username}, {$push: {rate: {user_id: result.login_user, rate: rate}}}, function (err) {
                            if (err) {
                                log.error('Feedback - User %s - Error with %s'.red, result.login_user,err);
                                res.send({authen_status: 'ok', status: 'error'});
                            } else {
                                if (feedback == null) {
                                    log.info('Feedback - User %s - OK'.blue, result.login_user);
                                    res.send({authen_status: 'ok', status: 'ok'});
                                } else {
                                    db.host.update({user_id: host_username}, {$pull: {feedback: {username: result.login_user}}}, function (err) {
                                        if (err) {
                                            log.error('Feedback - User %s - Error with %s'.red, result.login_user,err);
                                            res.send({authen_status: 'ok', status: 'error'});
                                        } else {
                                            db.host.update({user_id: host_username}, {$push: {feedback: {username: result.login_user, feedback: feedback}}}, function (err) {
                                                if (err) {
                                                    log.error('Feedback - User %s - Error with %s'.red, result.login_user,err);
                                                    res.send({authen_status: 'ok', status: 'error'});
                                                } else {
                                                    log.info('Feedback - User %s - OK'.blue, result.login_user);
                                                    res.send({authen_status: 'ok', status: 'ok'});
                                                }
                                            });
                                        }
                                    });
                                }
                            }
                        });
                    }
                });
            }
        });
    });

};